package cn.zhou.jin.service.impl;

import cn.zhou.basic.service.impl.BaseServiceImpl;
import cn.zhou.basic.util.MD5Utils;
import cn.zhou.basic.util.StrUtils;
import cn.zhou.jin.domain.Employee;
import cn.zhou.jin.mapper.EmployeeMapper;
import cn.zhou.jin.service.IEmployeeService;
import cn.zhou.user.domain.Logininfo;
import cn.zhou.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional (readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    //1.基础的CRUD实现通过继承BaseServiceImpl 得到

    //2.特殊的接口实现在这里自己写

    @Override
    public void add(Employee employee) {
        initEmployee(employee);
        //要级联保存logininfo
        Logininfo logininfo = emp2Logininfo(employee);
        logininfoMapper.add(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.add(employee);
    }

    private Logininfo emp2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//注意：一定要手动设置，这是员工
        return logininfo;
    }

    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);


        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
    }

    @Override
    public void update(Employee employee) {
        super.update(employee);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }

    @Override
    public Employee loadByLogininfoId(Long id) {
        return employeeMapper.loadByLogininfoId(id);
    }

    @Override
    public List<Employee> queryshopById(Long shopId) {
        return employeeMapper.queryshopById(shopId);
    }


//    @JsonInclude(JsonInclude.Include.NON_EMPTY)
//    @Override
//    public List<Employee> queryTree() {
//        // 这个是拿到所有的数据
//        List<Employee> all = employeeMapper.loadAll();
//        // 创建一个map 对象来装
//        Map<Long,Employee> employeeg= new HashMap<>();
//        // 遍历一下    把拿到数据装进 map
//        for (Employee employee:all){
//            employeeg.put(employee.getId(),employee);
//        }
//        // 创建一个数组
//       List<Employee> arrayList = new ArrayList<>();
//        for(Employee tmp:all){
//            //遍历 把如果里面的数组的parent id 是空 那么就是祖宗 单独提出来
//            if(tmp.getParent_id()==null){
//                // 添加到数组中去
//                arrayList.add(tmp);
//            }else {
//                // 不是祖宗，那么就是儿子 把自己当成儿子去找爹    即 prent id 有值
//                Employee parent=employeeg.get(tmp.getParent_id());
//                if (parent!=null){
//                    parent.getChildren().add(tmp);
//                }
//            }
//        }
//
//
//        return all;







//    }
}